<?php

/**
 * login actions.
 *
 * @package    elearning
 * @subpackage login
 * @author     dvquan
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 * Created		2010/09/08
 * updated		2010/09/09
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    //$this->forward('default', 'module');
	if ( $request->hasParameter( 'txtValue' ) )
		$this->Error = $request->getParameter('txtValue');
	$ls_error = $this->getUser()->getFlash('flash_error');
	if($ls_error)
		$this->Error = $ls_error;
	else
		$this->Error = '';
  }
  //execute login
  public function executeShow(sfWebRequest $request)
  {
    $logtype = "" . $request->getParameter( 'logtype', "0" );
	$userName = $request->getParameter( 'txtUsername', "0" );
	$password = $request->getParameter( 'txtPassword', '0' );
	$li_rem	=	$request->getParameter( 'remember', '0' );
	$lb_rem	=	false;
	if( $li_rem == '1' )
		$lb_rem = true;
	$student = 'student';
	$teacher = 'teacher';
	$this->type = " khong biet";
	if( strcmp ( $logtype ,$student ) == 0 )
	{
		if(TeacherPeer::chk_StudentByAccount( $userName, $password, 1 ) == 1)
		{
			$this->loginProcess( $userName, $lb_rem ); 
		}
		else
		{
			$this->getUser()->setFlash('flash_error', ' username or password invalid.');
			$this->getUser()->setAuthenticated(false);
			return $this->forward('login', 'index');
		}
	}
	else 
		if( strcmp ( $logtype ,$teacher) == 0 )
		{
			if(TeacherPeer::chk_TeacherByAccount( $userName, $password, 1 ) == 1)
			{
				$this->loginProcess( $userName, $lb_rem ); 
			}
			else
			{
				$this->getUser()->setFlash('flash_error', ' username or password invalid.');
				$this->getUser()->setAuthenticated(false);
				return $this->forward('login', 'index');
			}
			//
		}
  }
  //execute logout use: login/logout to call logout
  public function executeLogout(sfWebRequest $request)
  {
  	$this->logoutProcess();
  }
  public function logoutProcess( )
  {
  	$this->getUser()->setAuthenticated(false);
	//remove user name session
	$this->getUser()->getAttributeHolder()->remove('username');
	sfContext::getInstance()->getResponse()->setCookie('vnAuthen', '', time() - 36000, '/');
	//return the login
	$this->redirect('@login');
  }
  public function loginProcess( $username, $remember = false )
  {
  	//set cookie
	$this->getUser()->setAuthenticated(true);
	// Store username in the user session
    $this->getUser()->setAttribute('username', $username);

	if( $remember )
	{
		$value = base64_encode( serialize( array( $username, '1' ) ) );
		sfContext::getInstance()->getResponse()->setCookie('vnAuthen', $value, time() + 60*60*24*15);
	}
	$this->redirect('@homepage');
	// $value = base64_encode( serialize( array( value, value ) ) )	;
	// sfContext::getInstance()->getResponse()->setCookie('', $value, time() + 60*60*24*15, '/');

	//$value = unserialize( base64_decode( $this->getRequest()->getCookie( '' ) ) );
	//access $value[0], $value[1]
  }
}
